<?php
define( '__ESPACE__', 'admin');
$prefix = '../../';
require_once($prefix.'starter.php');

ob_start();

$PWE_admin = new controler_admin();

$publication = null;
$tag = null;
if(isset($_GET['publication_id']) && $_GET['publication_id']>0){
    $publication = new modele_publication($_GET['publication_id']);
}
if(isset($_GET['tag_id']) && $_GET['tag_id']>0){
    $tag = new modele_tag($_GET['tag_id']);
}
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Traduction</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="robots" content="noindex,nofollow">
        <link href='http://fonts.googleapis.com/css?family=Grand+Hotel' rel='stylesheet' type='text/css'>
        <link href="../css/style.css" type="text/css" rel="stylesheet" media="all"/>
        <script src="../../js/libs/jquery/jquery.js" type="text/javascript"></script>
        <link href="<?php echo $PWE_admin->getUiThemePath(); ?>" type="text/css" rel="stylesheet" media="all"/>
        <script src="../../js/libs/jqueryui/jquery-ui.js" type="text/javascript"></script>
        <script src="<?php echo $PWE_admin->getBaseURL(); ?>js/libs/ckeditor/ckeditor.js" type="text/javascript"></script>
        <script>
            var baseUrl = "<?php echo $PWE_admin->getBaseURL(); ?>";
            var themePath = "<?php echo $PWE_admin->getThemePath(); ?>";
            <?php if(file_exists($PWE_admin->getThemePath().'/admin/js/styles.js')): ?>
            var ckeditorStylesDeclaration = 'theme:../../<?php echo $PWE_admin->getThemePath(); ?>/admin/js/styles.js';
            <?php else: ?>
            var ckeditorStylesDeclaration = 'default:./styles.js';
            <?php endif; ?>
        </script>
        
    <?php if(isset($_POST) && isset($_POST['submit'])): ?>
        <?php
            $operation = false;
            if($_GET['action']==='delete'){
                $tag->removePublication($publication->getId());
                $operation = true;
            }else{
                if($_POST['tagId']>0){
                    $tagToAdd = new modele_tag($_POST['tagId']);
                    $tagToAdd->addPublication($publication->getId());
                    $operation = true;
                }elseif($_POST['tagName']!==''){
                    $tagToCreate = new modele_tag();
                    $datas = array(
                        'title'=>$_POST['tagName']
                        ,'template'=>'fullwidth-content'
                        ,'lien'=>  tools_convert::slug($_POST['tagName'])
                    );
                    
                    $tagToCreate->setDatas($datas);
                    $tagToCreate->save();
                    $tagToCreate->addPublication($publication->getId());
                    $operation = true;
                }
            }
        ?>
        <script type="text/javascript">
            var origine = window.top;
            <?php if($operation===true): ?>
            origine.closeDialogBox(true);
            <?php endif; ?>
        </script>
    </head>
    <body>
        <?php if($operation===true): ?>
        modification effectuée
        <?php else: ?>
        erreur d'exécution
        <?php endif; ?>
    </body>
    <?php else: ?>
        <script type="text/javascript">
            var origine = window.top;
            
            $(document).ready(function(){
                $('.pwe-ui-btn').button();
                $('form').submit(function(){
                    if( $('#content').val() !== '' ){
                        return true;
                    }else{
                        return false;
                    }
                });
            });
        </script>
    </head>
    <body>
        <form method="post" enctype="multipart/form-data" action="?action=<?php echo $_GET['action']; ?>&publication_id=<?php echo $_GET['publication_id']; ?><?php echo (isset($_GET['tag_id']))?'&tag_id='.$_GET['tag_id']:''; ?>">
            <?php if($_GET['action']==='delete'): ?>
            <input id="submit"  type="submit" name="submit" value="supprimer le tag" class="pwe-ui-btn"/>
            <?php else: ?>
            <script>
            var tags = [
                    <?php
                    $listeTag = modele_tag::getPrimaryListe();
                    foreach($listeTag as $i=>$tag): ?>
                    {value:'<?php echo $tag->getId(); ?>',label:'<?php echo $tag->getTitle(); ?>'}<?php echo(($i+1)===count($listeTag))?'':','; ?>
                    <?php endforeach; ?>
            ];
                $(document).ready(function(){
                    $('#tagName').autocomplete({
                        minLength: 1
                        ,source: tags
                        ,select: function(event, ui){
                            $( "#tagId" ).val( ui.item.value );
                            $( "#tagName" ).val( ui.item.label );
                            return false;
                        }
                    });
                });
            </script>
            <input type="text" id="tagName" name="tagName" value="" />
            <input type="hidden" id="tagId" name="tagId" value="0" />
            <input id="submit"  type="submit" name="submit" value="enregistrer" class="pwe-ui-btn"/>
            <?php endif; ?>
        </form>
    </body>
    <?php endif; ?>
</html>
<?php ob_end_flush(); ?>